layui.define(function(exports) {
    layui.use(['form'], function () {
        let form = layui.form, layer = layui.layer, $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        let parentIndex = layer.index;

        /**
         * 修改图书信息
         */
        function editBook(){
            let confirmIndex = layer.confirm('确定修改图书信息吗？', {
                btn: ['确定', '取消']
            }, function () {
                let loadIndex = layer.load(0, {
                    shade: [0.3, '#8B8682']
                });
                let bookNo = $("#bookNoEdit").val();
                let bookName = $("#bookNameEdit").val();
                let publish = $("#publishEdit").val();
                $.ajax({
                    url: "book/updateBook",
                    data: {
                        bookNo: bookNo,
                        bookName: bookName,
                        publish: publish
                    },
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        console.log(data);
                        if(data.code === -1){
                            layer.alert(data.msg, {
                                icon: 2,
                                skin: 'layer-ext-moon'
                            })
                        }else{
                            layer.alert(data.msg, {
                                icon: 1,
                                skin: 'layer-ext-moon'
                            })
                            // 关闭弹出层
                            layer.close(confirmIndex);
                            layer.close(parentIndex);
                            parent.layui.table.reload('bookTableId');//重载父页表格，参数为表格ID
                        }
                    },
                    error: function (data) {
                        layer.alert(data.responseText, {
                            icon: 2,
                            skin: 'layer-ext-moon'
                        })
                    }
                });
                // 关闭遮罩
                layer.close(loadIndex);
            }, function () {
                layer.close(confirmIndex);
            });
        }

        //监听提交
        form.on('submit(editBtn)', function (data) {
            editBook();
        });
    });
})
